#include "yshiftsetting.h"
#include "ui_yshiftsetting.h"

YShiftSetting::YShiftSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::YShiftSetting)
{
    ui->setupUi(this);
}

YShiftSetting::~YShiftSetting()
{
    delete ui;
}

void YShiftSetting::setOption(Shift &shift, QCustomPlot *plot, QVector<double> *origin) {
    ui->spinOffset->setValue(shift.offset);
    ui->spinOffset->setSingleStep(shift.step);
    mOrigin = origin;
    mPlot = plot;
}

QPair<double, double> YShiftSetting::getOption() {
    return {ui->spinOffset->value(), ui->spinOffset->singleStep()};
}

void YShiftSetting::shiftData(QCustomPlot *plot, QVector<double> *origin, double offset) {
    if (!plot || !origin) {
        return;
    }

    auto graph = plot->graph(0);
    if (!graph) {
        return;
    }

    auto size = graph->dataCount();
    if (size != origin->size()) {
        return;
    }

    QVector<double> index(size);
    QVector<double> value(size);
    for (int i = 0; i < size; ++i) {
        index[i] = i;
        value[i] = origin->at(i) + offset;
    }

    graph->setData(index, value, true);
    plot->replot(QCustomPlot::rpQueuedReplot);
}

//void YShiftSetting::on_spinOffset_valueChanged(double arg1)
//{
//    shiftData(mPlot, mOrigin, arg1);
//}

void YShiftSetting::on_btnDefault_clicked()
{
    ui->spinOffset->setValue(0);
//    shiftData(mPlot, mOrigin, 0);
    accept();
}

